package com.ryx.controller;

import com.ryx.aop.annotation.MyLog;
import com.ryx.entity.SysPermission;
import com.ryx.service.PermissionService;
import com.ryx.utils.DataResult;
import com.ryx.vo.req.PermissionAddReqVo;
import com.ryx.vo.req.PermissionUpdateReqVo;
import com.ryx.vo.resp.PermissionRespNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @BelongsPackage: com.ryx.controller
 * @Author: 容永轩
 * @CreateTime: 2020-12-23
 * @Description:
 */
@RestController
@RequestMapping("/api")
@Api(value = "组织部门菜单权限管理")
public class PermissionController {

    @Autowired
    private PermissionService permsService;

    @GetMapping("/permissions")
    @MyLog(title = "组织管理-菜单权限管理",action = "获取所有的菜单权限数据接口")
    @RequiresPermissions("sys:permission:list")
    public DataResult<List<SysPermission>> setPermissions() {

        DataResult result = DataResult.success();
        result.setData(permsService.selectAll());

        return result;
    }

    @GetMapping("/permission/tree")
    @MyLog(title = "组织管理-菜单权限管理",action = "只递归查询到菜单接口")
    @RequiresPermissions(value = {"sys:permission:update","sys:permission:add"},logical = Logical.OR)
    public DataResult<List<PermissionRespNodeVo>> getAllPermissionTrees() {
        DataResult result = DataResult.success();

        result.setData(permsService.selectAllMenuByTree());

        return result;

    }

    @PostMapping("/permission")
    @ApiOperation(value = "新增菜单权限接口")
    @MyLog(title = "组织管理-菜单权限管理",action = "新增菜单权限接口")
    @RequiresPermissions("sys:permission:add")
    public DataResult<SysPermission> addPermission(@RequestBody @Valid PermissionAddReqVo vo) {
        DataResult result = DataResult.success();

        result.setData(permsService.addPermission(vo));

        return result;
    }

    //递归查询所有权限包括按钮
    @GetMapping("/permission/tree/all")
    @MyLog(title = "组织管理-菜单权限管理",action = "只递归查询所有接口")
    @RequiresPermissions(value = {"sys:role:update","sys:role:add"},logical = Logical.OR)
    public DataResult<List<PermissionRespNodeVo>> getAllTree() {
        DataResult result = DataResult.success();

        result.setData(permsService.selectAllTree());

        return result;

    }

    @PutMapping("/permission")
    @ApiOperation(value = "更新菜单权限接口")
    @MyLog(title = "组织管理-菜单权限管理",action = "编辑菜单权限接口")
    @RequiresPermissions("sys:permission:update")
    public DataResult updatePermission(@RequestBody @Valid PermissionUpdateReqVo vo) {
        DataResult result = DataResult.success();
        permsService.updatePermission(vo);
        return result;
    }

    @DeleteMapping("/permission/{permissionId}")
    @ApiOperation(value = "删除菜单权限接口")
    @MyLog(title = "组织管理-菜单权限管理",action = "删除菜单权限接口")
    @RequiresPermissions("sys:permission:delete")
    public DataResult deletePermission(@PathVariable String permissionId){
        DataResult result =DataResult.success();
        permsService.deletedPermission(permissionId);

        return result;
    }

}
